function formatterAdmin(value,row,index) {
    return value?"是":"否";

}
function formatterloginCount(value,row,index) {
    return value?"在职":"离职";

}
function formatterPosition(value,row,index) {
    console.log(value);
   if(value){
       return value.name;
   }

}
function formatterEmployee(value,row,index) {
    console.log(value);
   if(value){
       return value.name;
   }

}

$(function () {
    var operator_datagrid = $("#operator_datagrid");
    var operator_dialog = $("#operator_dialog");
    var operator_add_form = $("#operator_add_form");
    var operator_combobox = $("#operator_combobox");
    var employee_operator = $("#employee_operator");
    var combobox_role = $("#combobox_role");

    operator_datagrid.datagrid({
        url: '/operator/list',
        fit: true,
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        singleSelect: true,
        columns: [
            [
                {field: 'username', title: '账号', width: 100},
                {field: 'employee', title: '姓名', width: 100,formatter:formatterEmployee},
                {field: 'role', title: '职位', width: 100,formatter:formatterPosition},
                {field: 'state', title: '状态', width: 100,formatter:formatterloginCount},
                {field: 'inputTime', title: '添加时间', width: 100},
                {field: 'loginCount', title: '登陆次数', width: 100},
                {field: 'admin', title: '超管', width: 100,formatter:formatterAdmin},
            ]
        ]
    })
    operator_dialog.dialog({
        width: 320,
        height: 320,
        closed: true,
        onClose: function () {
            operator_add_form.form('clear')
        }
    })

    var methodObj = {
        add: function () {
            operator_dialog.dialog('open');
            operator_dialog.dialog('setTitle', '新增员工');
            $("#pwd").show();
    },
        edit: function () {
            var row = operator_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选择一条数据', 'warning');
                return;
            }
            if(row.employee){
                row['employee.id']=row.employee.id;
            }
            if(row.role){
                row['role.id']=row.role.id;
            }
            //处理菜单的回显,通过角色的id查询权限的id集合,调用combotree的setValue方法
            $.post("/role/listIdsByOperatorId",{operatorId:row.id},function (data) {
                $("#operator_combobox").combobox("setValue",data);//设置为true
            })
            operator_add_form.form('load', row);
            operator_dialog.dialog('open');
            $("#pwd").hide();
            operator_dialog.dialog('setTitle', '编辑员工');
        },

        reload: function () {
            operator_datagrid.datagrid('reload');
        },
        remove: function () {
            var selectedRow = operator_datagrid.datagrid("getSelected");

            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "请选择一行数据", "error");
                return;
            }
            //给出提示确认要删除吗
            $.messager.confirm("提示", "您确定要删除吗?", function (yes) {
                if (yes) {

                    //发送ajax请求执行删除
                    $.post("/operator/delete", {id: selectedRow.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                methodObj.reloadEmp();
                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }
                    });
                }
            })
        },
        //取消
        cancel: function () {
            //关闭添加弹窗
            operator_dialog.dialog("close");
            methodObj.reload();
        },
        //提交保存
        save: function () {
            var url = "/operator/save";

            //获取员工的id
            var operatorId = $("#operator_id").val();
            if (operatorId) {
                //编辑的url
                url = "/operator/update";
            }

            //提交表单
            operator_add_form.form("submit", {
                url: url,
                onSubmit: function (param) {
                    //手动获取右边的数据
                    var rows = operator_combobox.combobox("getValues");
                    //获取已经选中的数据
                    //迭代,拼接成后台需要的数据
                    for (var i = 0; i < rows.length; i++) {
                        //取到每一行数据
                        param['roles[' + i + '].id'] = rows[i];
                    }
                    /*//手动获取数据
                    var values = employee_operator.combobox("getValues");
                    //获取已经选中的数据
                    //迭代,拼接成后台需要的数据
                    for (var i = 0; i < values.length; i++) {
                        //取到每一行数据
                        param['employee[' + i + '].id'] = values[i];
                    }*/
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            methodObj.cancel();
                            methodObj.reload();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }


            })
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})

